package com.rb.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTimeUtil {

	/**
	 * 格式化unix日期
	 * 
	 * @param time
	 * @param format
	 * @return
	 */
	public static String formatUnix(long time, String format) {
		if (format == null)
			format = "yyyy-MM-dd kk:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date(time * 1000));
	}

	/**
	 * 获取当前时间
	 * 
	 * @param format
	 * @return
	 */
	public static String getCurrentTime(String format) {
		if (format == null)
			format = "yyyy-MM-dd kk:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date(System.currentTimeMillis()));
	}

	public static Timestamp getCurrentStampTime() {
		return new Timestamp(System.currentTimeMillis());
	}

	/**
	 * 格式化时间，默认“yyyy-MM-dd”
	 * 
	 * @param time
	 * @return
	 */
	public static String formatUnix(long time) {
		return formatUnix(time, null);
	}

	public static String formatUnix(Timestamp ts) {
		long time = System.currentTimeMillis() / 1000;
		if (ts != null)
			time = ts.getTime() / 1000;
		return formatUnix(time, null);
	}

	public static long getTime(Timestamp ts) {
		long time = System.currentTimeMillis() / 1000;
		if (ts != null)
			time = ts.getTime() / 1000;
		return time;
	}

	/**
	 * 获取时间戳，根据日期
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static long getUnixTimeStamp(String date, String format) {
		if (format == null)
			format = "yyyy-MM-dd kk:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			Date d = sdf.parse(date);
			if (d != null)
				return getUnixTimeStamp(d);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public static long getUnixTimeStamp() {
		return System.currentTimeMillis() / 1000;
	}

	public static long getUnixTimeStamp(Date date) {
		return date.getTime() / 1000;
	}

	/**
	 * 获取用户年龄
	 * 
	 * @param timeStamp
	 * @return
	 */
	public static int getAge(long timeStamp) {
		Date beginDate = new Date(timeStamp * 1000);
		Date endDate = new Date(System.currentTimeMillis());
		int age = (int) ((endDate.getTime() - beginDate.getTime()) / 24 / 60 / 60 / 1000 / 365);
		return age;
	}

	public static int getSecondToNow(long timeStamp) {
		Date beginDate = new Date(timeStamp * 1000);
		Date endDate = new Date(System.currentTimeMillis());
		return (int) ((endDate.getTime() - beginDate.getTime()) / 1000);
	}

	/**
	 * 获取时间
	 * 
	 * @param mills
	 * @return
	 */
	public static String getDuration(long mills) {
		long s = mills;
		if (s > 60) {
			int m = (int) (s / 60);
			if (m > 60) {
				int h = m / 60;
				return get0Num(h) + get0Num(m % 60) + get0Num((int) s % 60);
			} else {
				return "00:" + get0Num(m) + ":" + get0Num((int) s % 60);
			}
		} else {
			return "00:" + get0Num((int) s);
		}
	}

	public static String get0Num(int n) {
		return n < 10 ? "0" + n : String.valueOf(n);
	}

	/**
	 * 格式化
	 * 
	 * @param time
	 * @return
	 */
	public static String formatUnixTimeStamp(long now, long time) {
		long delay = now - time;
		if (delay < 60) {
			return "刚刚";
		} else if (delay < 3600) {
			int min = (int) (delay / 60);
			return min + "分钟前";
		} else if (delay < 3600 * 24) {
			int hour = (int) (delay / 3600);
			return hour + "小时前";
		} else {
			return formatUnix(time, "yyyy-MM-dd ");
		}
	}

}
